package com.example.smartall.fragment;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.smartall.R;
import com.example.smartall.api.Api;
import com.example.smartall.api.ApiConfig;
import com.example.smartall.api.NetCallBack;
import com.example.smartall.entity.NewsBean;
import com.example.smartall.fragment.base.BaseFragment;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.charts.HorizontalBarChart;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.DataSet;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.data.PieEntry;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

/**
  * @Author:         abbas
  * @CreateDate:     2022/2/13
  * @Description:    数据分析模块 如果不搞图表配置 光数据设置看起来好像基本是Ctrl C V
  */
public class DAFragment extends BaseFragment {

    private LineChart line;
    private BarChart bar;
    private HorizontalBarChart horizontalBar;
    private PieChart pie;

    private List<NewsBean.RowsBean> rowsBeans = new ArrayList<>();

    private List<Entry> entries = new ArrayList<>();

    private List<BarEntry> barEntries = new ArrayList<>();

    private List<PieEntry> pieEntries = new ArrayList<>();

    private MHandler handler;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        handler = new MHandler();

        View view = inflater.inflate(R.layout.fragment_d_a, container, false);
        init(view);
        initEvent();
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        if (rowsBeans.size() > 0){
            handler.sendEmptyMessage(0x01);
            return;
        }
        getData();
    }

    private void init(View view) {
        line = (LineChart) view.findViewById(R.id.line);
        bar = (BarChart) view.findViewById(R.id.bar);
        horizontalBar = (HorizontalBarChart) view.findViewById(R.id.horizontalBar);
        pie = (PieChart) view.findViewById(R.id.pie);

    }

    private void initEvent() {
    }

    private void getData(){
        if (rowsBeans != null && rowsBeans.size() > 0) return;
        Api.config(ApiConfig.NEWS_URL,null).getRequest(new NetCallBack() {
            @Override
            public void onSuccess(String res) {
                NewsBean bean = new Gson().fromJson(res,NewsBean.class);
                if (bean != null){
                    rowsBeans = bean.getRows();
                    handler.sendEmptyMessage(0x01);
                }
            }

            @Override
            public void onFailure(Exception e) {

            }
        },"?pageNum=1&pageSize=5");
    }

    class MHandler extends Handler{
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 0x01:
                    setLineChart();
                    setBarChart();
                    setHorizontalBarChart();
                    setPieChart();
                    break;
            }
        }
    }

    private void setPieChart() {
        for (int i = 0; i < rowsBeans.size(); i++) {
            pieEntries.add(new PieEntry(rowsBeans.get(i).getLikeNum()));
        }
        PieDataSet dataSet = new PieDataSet(pieEntries,"News");
        PieData data = new PieData(dataSet);
        pie.setData(data);
        pie.invalidate();
    }

    private void setHorizontalBarChart() {
        BarDataSet dataSet = new BarDataSet(barEntries,"News");
        BarData data = new BarData(dataSet);
        horizontalBar.setData(data);
        bar.invalidate();
    }

    private void setBarChart() {
        for (int i = 0; i < rowsBeans.size(); i++) {
            barEntries.add(new BarEntry((float) (i + 1),(float) rowsBeans.get(i).getLikeNum()));
        }
        BarDataSet dataSet = new BarDataSet(barEntries,"News");
        BarData data = new BarData(dataSet);
        bar.setData(data);
        bar.invalidate();
    }

    private void setLineChart() {
        for (int i = 0; i < rowsBeans.size(); i++) {
            entries.add(new Entry(i+1,rowsBeans.get(i).getLikeNum()));
        }
        LineDataSet dataSet = new LineDataSet(entries,"News");
        LineData data = new LineData(dataSet);
        line.setData(data);
        line.invalidate();
    }
}